package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.microsoft.schemas.office.visio.x2012.main.CellType;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.dto.ErpCustomerDto;
import com.nuoniu.sibanyun.entity.erp.ErpCustomer;
import com.nuoniu.sibanyun.mapper.erp.ErpCustomerMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpCustomerService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 白强
 * @since 2021-01-23
 */
@Service
public class ErpCustomerServiceImpl extends ServiceImpl<ErpCustomerMapper, ErpCustomer> implements IErpCustomerService {

    @Autowired
    private IErpAccountService erpAccountService;

    @Override
    public Page<ErpCustomer> pageList(ErpCustomerDto erpCustomerDto) {
        Integer currentAccountId = erpAccountService.getCurrentAccountId();
        UserInfo userInfo = UserThreadLocal.get();
        Page page = erpCustomerDto.getPage();
        QueryWrapper<ErpCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("account_id",currentAccountId);
        String keyWord = erpCustomerDto.getKeyWord();
        if(StringUtils.isNotBlank(keyWord)){
            queryWrapper.and(wrapper -> wrapper.like("customer_code", keyWord)
                    .or().like("customer_name", keyWord)
                    .or().like("customer_pym", keyWord));
        }
        if(StringUtils.isNotBlank(erpCustomerDto.getCustomerCode())){
            queryWrapper.like("customer_code",erpCustomerDto.getCustomerCode());
        }
        if(StringUtils.isNotBlank(erpCustomerDto.getCustomerName())){
            queryWrapper.like("customer_name",erpCustomerDto.getCustomerName());
        }
        if(StringUtils.isNotBlank(erpCustomerDto.getCustomerPym())){
            queryWrapper.like("customer_pym",erpCustomerDto.getCustomerPym());
        }
        page = page(page, queryWrapper);
        return page;
    }

    @Override
    public void saveErpCustomer(ErpCustomer erpCustomer) {
        Integer currentAccountId = erpAccountService.getCurrentAccountId();
        UserInfo userInfo = UserThreadLocal.get();
        Integer companyId = userInfo.getCompanyId();
        Long id = erpCustomer.getId();
        if(null == id){
            erpCustomer.setStatus(0);
            erpCustomer.setCreateUserId(userInfo.getUserId());
            erpCustomer.setCreateUserName(userInfo.getRealName());
            erpCustomer.setAccountId(currentAccountId);
            erpCustomer.setCompanyId(companyId);
        }else{
            erpCustomer.setUpdateUserId(userInfo.getUserId());
            erpCustomer.setUpdateUserName(userInfo.getRealName());
        }
        boolean isRepeat = baseMapper.isRepeat(companyId, currentAccountId, erpCustomer.getCustomerName(), id);
        if(isRepeat) throw new NuoNiuException("该客户档案记录已经存在");
        saveOrUpdate(erpCustomer);
    }

    //
    @Override
    public void delete(Integer id) {
        if(null == id)throw new NuoNiuException("参数错误");
        //todo 判断是否已经使用
        ErpCustomer erpCustomer = getById(id);
        erpCustomer.setStatus(1);
        updateById(erpCustomer);
    }

    @Override
    public boolean isUsedClass(Integer partnerClassId) {
        Integer currentAccountId = erpAccountService.getCurrentAccountId();
        UserInfo userInfo = UserThreadLocal.get();
        Integer companyId = userInfo.getCompanyId();
        QueryWrapper<ErpCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        queryWrapper.eq("account_id",currentAccountId);
        queryWrapper.eq("partner_class_id",partnerClassId);
        int count = count(queryWrapper);
        return count > 0;
    }

    public void importData(MultipartFile file)  throws IOException{

    }
}
